package com.one.efaimaly.user.presenter;

import android.content.Context;

import com.one.common.model.bean.CityBean;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.ImageFactory;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.base.BaseActivity;
import com.one.common.common.user.modle.UserInfoModel;
import com.one.efaimaly.user.model.UserWorkerModel;
import com.one.common.model.bean.UserWorkerBean;
import com.one.efaimaly.user.view.UserWorkerView;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.one.common.model.http.NetConstant.IMG_HOST;

/**
 * creator: zhulunjun
 * time:    2018/8/6
 * describe: 师傅信息修改
 */
public class UserWorkerPresenter extends BaseApiPresenter<UserWorkerView, UserWorkerModel> {

    private boolean isUpdate = false, isWorkerUpdate = false;

    public UserWorkerPresenter(UserWorkerView mView, Context mContext) {
        super(mView, mContext, new UserWorkerModel((BaseActivity) mContext));
    }

    public void getUserWorkerInfo() {
        mModel.getUserWorkerInfo(new ObserverOnResultListener<UserWorkerBean>() {
            @Override
            public void onResult(UserWorkerBean userWorkerBean) {
                if (userWorkerBean != null) {
                    mView.setUserWorkerView(userWorkerBean);
                }
            }
        });
    }

    /**
     * 解析参数
     */
    public void update(String avatarUrl, String name, String contact,
                       String contactPhone, String provider,
                       String city, String country) {
        Map<String, String> params1 = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        if (StringUtils.isNotBlank(avatarUrl)) {
            params1.put("member_img", avatarUrl);
        }
        if (StringUtils.isNotBlank(name)) {
            params1.put("member_real_name", name);
        }
        if (StringUtils.isNotBlank(contact)) {
            params2.put("member_name", contact);
        }
        if (StringUtils.isNotBlank(contactPhone)) {
            params2.put("member_phone", contactPhone);
        }
        if (StringUtils.isNotBlank(provider)) {
            params2.put("member_provider", provider);
        }
        if (StringUtils.isNotBlank(city)) {
            params2.put("member_city", city);
        }
        if (StringUtils.isNotBlank(country)) {
            params2.put("member_country", country);
        }

        if (params1.size() > 0) {
            updateUserInfo(params1);
        }
        if (params2.size() > 0) {
            updateUserWorker(params2);
        }

    }

    /**
     * 修改师傅特有用户信息
     */
    private void updateUserWorker(Map<String, String> params) {
        mModel.updateWorkerUserInfo(params, new ObserverOnResultListener<String>() {
            @Override
            public void onResult(String s) {
                isWorkerUpdate = true;
                if (isUpdate) {
                    if (mView != null) {
                        mView.finishPage();
                    }
                }

            }

        });
    }

    /**
     * 修改用户信息，头像和姓名
     */
    private void updateUserInfo(Map<String, String> params) {
        new UserInfoModel(mActivity).updateUserInfo(params, new ObserverOnResultListener<String>() {
            @Override
            public void onResult(String s) {
                isUpdate = true;
                if (isWorkerUpdate) {
                    if (mView != null) {
                        mView.finishPage();
                    }
                }
            }
        });
    }

    /**
     * 上传图片
     */
    public void uploadImg(File file) {
        if (file.exists()) {
            new UserInfoModel(mActivity).uploadImg(getImageFile(file.getPath()), new ObserverOnResultListener<String>() {
                @Override
                public void onResult(String s) {
                    String url = IMG_HOST + s;
                    if (mView != null) {
                        mView.setAvatarUrl(url);
                    }
                    Logger.d("zlj  图片地址 " + url);
                }
            });
        }

    }

    /**
     * 缩放图片
     */
    private File getImageFile(String path){
        String outFilePath = mContext.getExternalCacheDir().getPath() + System.currentTimeMillis() + ".jpg";
        ImageFactory imageFactory = new ImageFactory();
        try {
            imageFactory.ratio(imageFactory.getBitmap(path), 480, 480, outFilePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      return new File(outFilePath);
    }

    /**
     * 获取城市
     */
    public void getCitys() {
        new UserInfoModel(mActivity).getCitys(new ObserverOnResultListener<ArrayList<CityBean>>() {
            @Override
            public void onResult(ArrayList<CityBean> cityBeans) {
                if (!ListUtils.isEmpty(cityBeans)) {
                    if (mView != null) {
                        mView.setCityView(cityBeans);
                    }
                }
            }
        });
    }

    /**
     * 获取选择的city
     */
    public CityBean getCityBean(CityBean bean, String city) {
        if (!ListUtils.isEmpty(bean.getCityBeans())) {
            for (CityBean bean1 : bean.getCityBeans()) {
                if (bean1.getName().equals(city)) {
                    return bean1;
                }
            }
        }
        return bean;
    }

    /**
     * 获取选择的city
     */
    public CityBean getCityBean(ArrayList<CityBean> beans, String city) {
        if (!ListUtils.isEmpty(beans)) {
            for (CityBean bean1 : beans) {
                if (bean1.getName().equals(city)) {
                    return bean1;
                }
            }
        }
        return null;
    }
}
